import lawBankTopBar from 'components/lawBankTopBar/lawBankTopBar'
import { getAccountantList, getSortList } from '@/api/lawBank'
import { hightLightText } from '@/utils/utils'
import { checkViewNorm } from '@/api/checkView.js'
export default {
  components: {
    lawBankTopBar,
  },
  data() {
    return {
      title: '会计法规库',
      backgroundUrl: require('@/static/img/lawBank/topBar_bg_02.png'),
      searchText: '',
      scrollTop: 0,
      sortList: [{
        id: 1,
        nid: 1,
        name: '热点法规'
      }, {
        id: 2,
        nid: 2,
        name: '中央法规'
      }, {
        id: 3,
        nid: 3,
        name: '地方法规'
      }, {
        id: 27,
        name: '海关综合文件'
      }, {
        id: 20,
        name: '国际跨境税收'
      }, {
        id: 28,
        name: '外汇文件'
      }, {
        id: 32,
        name: ' 12366热点问答'
      }, {
        nid: 4,
        name: '法规法条'
      }], //分类
      chooseTagId: 1,
      keyword: '',
      form: {
        id: '',
        page: 1,
        pagesize: 10,
        keyword: '', //	否	string	搜索字段
      },
      isLoading: false,
      loadMoreStatue: 'more',
      count: 0,
      lawList: [],
      isRefreshing: false,
      loadMoreStatus: 'more',
      chooseLevel1TypeName: '',
      levelList: [],
      level2List: [],
      chooseLevel1Index: 0,
      isSearch: false,
      showPopup: false,
      tipsMsg: '今日免费次数已用完，请您充值成为会员，以享受更多精彩的功能和服务。',
    };
  },
  methods: {
    hightLightText(text, keyword) {
      return hightLightText(text, keyword)
    },
    goSearch() {
      this.form.keyword = this.keyword
      this.form.id = this.keyword === '' ? this.chooseTagId : '';
      this.isSearch = this.keyword === '' ? false : true;
      this.lawList = [];
      this.form.page = 1;
      this.title = this.form.keyword ? '搜索结果' : '会计法规库';
      this.getList();
    },

    //二级分类切换
    chooseTag(e) {
      this.chooseTagId = e.id;
      this.form.id = e.id;
      this.form.page = 1;
      this.lawList = [];
      this.getList();
    },

    //请求列表数据
    getList() {
      var that = this
      if (this.form.page == 1) {
        this.lawList = [];
        this.isLoading = true;
      } else {
        this.loadMoreStatue = 'loading';
      }
      getAccountantList(this.form).then(res => {
        if (res.code == 1) {
          if (res.data.length > 0) {
            this.count = res.data[0].datacount;
          } else {
            this.count = this.lawList[0].datacount;
          }
          var newList = res.data; //本次请求获取到的新数据
          //如果新数据的数量达到单页数量,则认为还有更多数据
          if (newList.length >= that.form.pagesize) {
            that.loadMoreStatus = 'more';
          } else {
            that.loadMoreStatus = 'noMore';
          }
          this.lawList = [...this.lawList, ...newList];
          this.isRefreshing = false;
        } else {
          this.$tools.showToast(res.msg);
          this.loadMoreStatus = 'noMore';
        }
      })
    },

    //下拉刷新
    reload() {
      this.form.page = 1;
      this.lawList = [];
      this.isRefreshing = true;
      this.getList()
    },

    //加载更多
    loadMore() {
      if (this.loadMoreStatus == 'more') {
        this.form.page++;
        this.getList()
      } else {
        this.$used.msg('没有更多了');
      }
    },

    /**获取会计法规分类**/
    getSortList() {

      this.isLoading = true;
      getSortList().then(res => {
        if (res.code == 1) {
          let data = res.data
          for (let key in data) {
            this.levelList.push({
              id: data[key].oneclass.id,
              name: data[key].oneclass.name,
              level2List: data[key].twoclass
            })
          }
          let id = this.levelList[0].level2List[0].id
          this.chooseLevel1TypeName = this.levelList[0].name;
          this.level2List = this.levelList[0].level2List;
          this.chooseTagId = id;
          this.form.id = id
          this.getList()
        }
      })
    },
    //切换一级分类
    chooseLevel(e) {
      console.log(e.detail.value);
      // 选中的一级分类下标
      let index = e.detail.value;
      this.chooseLevel1Index = index;
      this.chooseLevel1TypeName = this.levelList[index].name;
      // 默认选中二级分类第一个
      let id = this.levelList[index].level2List[0].id

      //选中一级分类后的二级分类列表
      this.level2List = this.levelList[index].level2List;
      this.chooseTagId = id;
      this.form.id = id
      this.lawList = [];
      this.form.page = 1;
      this.getList();
    },

    LevelCancel() {

    },
    //去详情
    toInfo(id) {
      checkViewNorm({ id }).then(res => {
        if (res.code == -100) {
          this.showPopup = true
        } else {
          uni.navigateTo({
            url: "./accountantInfo?id=" + id
          })
        }
      })
    },

    //去会员中心
    goPlatform() {
      uni.navigateTo({
        url: "/pages/my/vip/index"
      })
    },
  },
  onReady() {

  },
  onLoad(options) {
    this.getSortList()
  },
  onShow() {
  }
}